//
//  BKShowImgCollectionView.m
//  bestkeep
//
//  Created by windCould on 16/11/10.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKShowImgCollectionView.h"
#import "BKLayoutImgCollectionViewCell.h"
#import "BKMyCommon.h"
@interface BKShowImgCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>

@property (nonatomic,strong)NSArray * defaultArr;

@end


@implementation BKShowImgCollectionView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpViews];
    }

    return self;
}

-(void)setUpViews
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    
    self.layout = layout;
    UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
 
    self.collectionView = collectionView;
    collectionView.backgroundColor = ThemeBackgroundColor;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.pagingEnabled = YES;
    collectionView.scrollsToTop = NO;
    collectionView.bounces = NO;
    collectionView.userInteractionEnabled = YES;
    collectionView.showsHorizontalScrollIndicator = NO;
    [collectionView registerClass:[BKLayoutImgCollectionViewCell class] forCellWithReuseIdentifier:LayoutImgCollectionViewCellId];
    [self addSubview:collectionView];
    [self layOutViews];
}

-(void)layOutViews
{
   [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
       
       make.top.mas_equalTo(self.mas_top).offset(0);
       make.left.mas_equalTo(self.mas_left).offset(0);
       make.right.mas_equalTo(self.mas_right).offset(0);
       make.bottom.mas_equalTo(self.mas_bottom).offset(0);
       
   }];

}

-(void)setImgArr:(NSArray *)imgArr
{
    _imgArr = imgArr;
    [_collectionView reloadData];
}

-(void)setIsDataOfImg:(BOOL)isDataOfImg
{
    _isDataOfImg = isDataOfImg;
    [_collectionView reloadData];
}

#pragma mark =========================UIScrowDelegate=================================

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;
    CGFloat width = point.x;
    NSInteger index = width/SCREENWIDTH;
    if ([self.myDelegate respondsToSelector:@selector(showImgCollectionView: collectionViewContentScrowToIndex:)]) {
        
        [self.myDelegate showImgCollectionView:self collectionViewContentScrowToIndex:index];
    }
}

#pragma mark ========================CollectionViewDelegate===========================

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.imgArr.count>3) {
        return 3;
    }
    return  self.imgArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BKLayoutImgCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:LayoutImgCollectionViewCellId forIndexPath:indexPath];
    
    if (_isDataOfImg) {
        
        cell.img = self.imgArr[indexPath.row];
    }else {
    
        cell.imgUrlStr = self.imgArr[indexPath.row];
    }
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return _itemSize;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([self.myDelegate respondsToSelector:@selector(showImgCollectionView: selectIndex:)]) {
        
        [self.myDelegate showImgCollectionView:self selectIndex:indexPath.item];
    }
    
}


@end
